package ar.edu.caece.pl.asem.model.impl.treeelements;

import ar.edu.caece.pl.asem.model.impl.SymbolTable;
import ar.edu.caece.pl.asem.temp.TemporalGenerator;

public class Parametro extends SimboloGenerico {

	public static final int BYREF = 0;
	public static final int BYVAL = 1;

	private int mode; // Solo para Parametros.
	private int memoryInStack;

	public Parametro(String name, int type, int mode) {
		super(name, type);
		this.mode = mode;
		this.label = "PARAMETRO";
	}

	public int getMode() {
		return mode;
	}

	public void setMode(int mode) {
		this.mode = mode;
	}
	
	public int getMemoryInStack() {
		return memoryInStack;
	}

	public void setMemoryInStack(int memoryInStack) {
		this.memoryInStack = memoryInStack;
	}

	@Override
	public String toString() {
		if (this.getMode() == BYREF) {
			return this.getLabel() + ": " + this.getName() + " (" + this.getEnvName() + " " + SymbolTable.getInstance().getVerboseType( this.getType()) + ") = BYREF";
		} else {
			return this.getLabel() + ": " + this.getName() + " (" + this.getEnvName() + " " + SymbolTable.getInstance().getVerboseType( this.getType()) + ") = BYVAL";
		}
	}

	@Override
	public void generateASMCode() {
		StringBuffer sb = new StringBuffer();
		if (!(this.parent instanceof Declaraciones || this.parent instanceof Procedimiento)) {
			TemporalGenerator.getInstance().generateTemporal();
			this.place = TemporalGenerator.getInstance().getActualTemporal();
			sb.append(TAB + "mov ax, " + "[bp+"+ memoryInStack +']' + ENTER);
			sb.append(TAB + "mov " + this.place + ",ax" + ENTER);
		}
		this.code = sb.toString();

	}

}
